package com.example.application4.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.application4.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class GridViewActivity extends AppCompatActivity {

    private static final String[] TITLES = {"日记", "图片管理", "好友", "画板"};
    private static final String[] INFOS = {"Http://www.baidu.com/", "Http://www.sina.com.cn/", "Http://www.qq.com/", "Http://www.163.com/"};
    private static final int[] IMAGES = {R.drawable.tb_netease, R.drawable.tb_sina, R.drawable.tb_sina, R.drawable.tb_sina};
    private static final Class<?>[] ACTIVITIES = {ListViewActivity.class, PhotoActivity.class, FriendListActivity.class, DrawActivity.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view); // 设置布局

        // 退出登录按钮
        findViewById(R.id.logout).setOnClickListener(v -> {
            // 清除登录信息
            getSharedPreferences("appPreferences", MODE_PRIVATE).edit()
                    .remove("username")
                    .remove("isLogin")
                    .apply();

            startActivity(new Intent(GridViewActivity.this, LoginActivity.class));
            finish();
        });

        // 获取 GridView 控件
        GridView grid = findViewById(R.id.gvNetInfo);
        // 创建 SimpleAdapter，绑定数据
        SimpleAdapter listItemAdapter = new SimpleAdapter(
                this, // 当前上下文
                getData(), // 数据源
                R.layout.griditem, // 单个网格项的布局
                new String[]{"img", "title"}, // 数据源中的键
                new int[]{R.id.img, R.id.title} // 布局中的控件 ID
        );
        // 设置适配器
        grid.setAdapter(listItemAdapter);
        // 添加单击监听器
        grid.setOnItemClickListener((parent, view, position, id) -> {
            try {
                // 获取点击项的数据
                HashMap<String, Object> clkmap = (HashMap<String, Object>) parent.getItemAtPosition(position);

                setTitle(clkmap.get("title").toString());

                Toast.makeText(GridViewActivity.this, clkmap.get("info").toString(), Toast.LENGTH_SHORT).show();

                // 根据位置启动相应的 Activity
                if (position >= 0 && position < ACTIVITIES.length) {
                    Intent intent = new Intent(GridViewActivity.this, ACTIVITIES[position]);
                    startActivity(intent);
                }
            } catch (Exception e) {
                Toast.makeText(GridViewActivity.this, "发生错误: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private List<HashMap<String, Object>> getData() {
        // 生成数据列表
        List<HashMap<String, Object>> data = new ArrayList<>();
        for (int i = 0; i < TITLES.length; i++) {
            HashMap<String, Object> item = new HashMap<>();
            item.put("img", IMAGES[i]); // 图片资源 ID
            item.put("title", TITLES[i]); // 标题
            item.put("info", INFOS[i]); // 信息
            data.add(item);
        }
        return data;
    }
}